package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.system.model.CommonUser;
import com.eling.elcms.travel.dao.IWelfareConsumptionCouponDao;
import com.eling.elcms.travel.dao.IWelfareConsumptionCouponDetailDao;
import com.eling.elcms.travel.model.*;
import com.eling.elcms.travel.model.WelfareConsumptionCoupon.Status;
import com.eling.elcms.travel.model.WelfareConsumptionCouponDetail.OperateStatus;
import com.eling.elcms.travel.model.view.WelfareConsumptionCouponStatisticsView;
import com.eling.elcms.travel.model.view.WelfareConsumptionCouponView;
import com.eling.elcms.travel.model.view.WelfareccView;
import com.eling.elcms.travel.service.IProductRuleDetailManager;
import com.eling.elcms.travel.service.ITravelContractManager;
import com.eling.elcms.travel.service.IWelfareConsumptionCouponManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WelfareConsumptionCouponManagerImpl extends
		GenericManagerImpl<WelfareConsumptionCoupon, Long> implements
		IWelfareConsumptionCouponManager {
	
	IWelfareConsumptionCouponDao consumptionCouponDao;
	@Autowired
	IWelfareConsumptionCouponDetailDao welfareConsumptionCouponDetailDao;
	
	@Autowired
	ITravelContractManager travelContractManager;
	
	@Autowired
	IProductRuleDetailManager productDetailManager;
	
	@Autowired
	public void setConsumptionCouponDao(
			IWelfareConsumptionCouponDao consumptionCouponDao) {
		this.dao = consumptionCouponDao;
		this.consumptionCouponDao = consumptionCouponDao;
	}
	
	@Autowired
	public void setWelfareConsumptionCouponDetailDao(
			IWelfareConsumptionCouponDetailDao welfareConsumptionCouponDetailDao) {
		this.welfareConsumptionCouponDetailDao = welfareConsumptionCouponDetailDao;
	}
	
	/**
	 * 补办
	 * @param cond
	 * @return 
	 * @author liyan
	 */
	@Override
	public WelfareConsumptionCoupon postregistered(WelfareConsumptionCoupon cond) {
		String cardNo = cond.getCardNo();
		String remark = cond.getRemark();
		List<WelfareConsumptionCoupon> query = query(cond);
		WelfareConsumptionCoupon wc = query.get(0);
		wc.setStatus(Status.Effective);
		wc.setCardNo(cardNo);
		wc.setReturnDate(new Date());
		wc.setRemark(remark);
		
		WelfareConsumptionCoupon save = save(wc);
		
		WelfareConsumptionCouponDetail wcd = new WelfareConsumptionCouponDetail();
		wcd.setWelfareConsumptionCoupon(save);
		wcd.setOperateStatus(OperateStatus.MendCard);
		wcd.setOperateDate(new Date());
		wcd.setOperator(save.getOperator()); 
		wcd.setServicePoint(cond.getServicePoint());
		welfareConsumptionCouponDetailDao.save(wcd);
		return save;
	}

	/**
	 * 查询待发放消费券的会员
	 * @param date
	 * @return
	 * @author ly
	 */
	@Override
	public List<WelfareConsumptionCouponView> queryWelfareCard(Date date){
		return consumptionCouponDao.queryWelfareCard(date);
	}
	
	/**
	 *  验证卡号是否已经被使用
	 * @return
	 * @author ly
	 */
	@Override
	public Integer queryWelfareCardAjax(String cardNo){
		return consumptionCouponDao.queryWelfareCardAjax(cardNo);
	}
	
	/**
	 * 根据条件的搜索
	 * @param sc
	 * @param value
	 * @return
	 * @author ly
	 */
	@Override
	public List<WelfareConsumptionCouponView> queryWelfareCardSerarch(SearchCondition sc){
		String s = sc.getS();
		return consumptionCouponDao.queryWelfareCardSerarch(s);
	}
	
	/**
	 * 根据条件的搜索
	 * @param sc
	 * @param cond
	 * @return
	 * @author ly
	 */
	@Override
	public List<WelfareConsumptionCoupon> queryWelfareCardSerarchGrid(SearchCondition sc,WelfareConsumptionCoupon cond){
		String s = sc.getS();
		String[] endDate = sc.getSearchProperties();
		List<Long> list = consumptionCouponDao.queryWelfareCardSerarchGrid(s,cond,endDate[0]);
		
		WelfareConsumptionCoupon wc = new WelfareConsumptionCoupon();
		List<WelfareConsumptionCoupon> search = new ArrayList<WelfareConsumptionCoupon>();
		List<WelfareConsumptionCoupon> returnList = new ArrayList<WelfareConsumptionCoupon>();
		 
		for(Long l : list){
			wc.setPkWelfarecc(l);
			search = consumptionCouponDao.query(wc);
			returnList.add(search.get(0));
		}
		return returnList;
	}

	@Override
	public WelfareConsumptionCoupon saveAll(Long pkWelfarecc, Double price,
			String remarkDetail) {
		WelfareConsumptionCoupon wel = consumptionCouponDao.get(pkWelfarecc);
		wel.setBalance(wel.getBalance()+price);
		wel.setRemark(remarkDetail);
		wel.setOperator((CommonUser)AppContext.curUser());
		wel.setReturnDate(new Date());
		wel.setIsGrant(true);
		//每年返券是把有限时间增加一年
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = sdf.format(wel.getEffectiveDate());
		String str1 = str.substring(0,4);
		Integer i =Integer.parseInt(str1);
		i=i+1;
		str1 = i.toString();
		str = str.replace((str.substring(0,4)).substring(0,4),str1);
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		wel.setEffectiveDate(date);
		wel = save(wel);
		WelfareConsumptionCouponDetail detail = new WelfareConsumptionCouponDetail();
		detail.setWelfareConsumptionCoupon(wel);
		detail.setOperateDate(new Date());
		detail.setOperator((CommonUser)AppContext.curUser());
		detail.setServicePoint(((CommonUser)AppContext.curUser()).getServicePoint());
		detail.setOperateStatus(WelfareConsumptionCouponDetail.OperateStatus.ReturnCertificate);
		detail.setPrice(price);
		welfareConsumptionCouponDetailDao.save(detail);
		return wel;
	}

	@Override
	public WelfareConsumptionCoupon saveCard(
			WelfareConsumptionCoupon welfareConsumptionCoupon) {
		welfareConsumptionCoupon.setEffectiveDate(calEffectiveDate(welfareConsumptionCoupon));
		WelfareConsumptionCouponDetail wccd = new WelfareConsumptionCouponDetail();
		wccd.setRemarkDetail(welfareConsumptionCoupon.getRemark());
		wccd.setOperateDate(welfareConsumptionCoupon.getGrantDate());
		wccd.setOperator(welfareConsumptionCoupon.getOperator());
		wccd.setServicePoint(welfareConsumptionCoupon.getServicePoint());
		wccd.setPrice(welfareConsumptionCoupon.getBalance());
		WelfareConsumptionCoupon wc = save(welfareConsumptionCoupon);
		wccd.setWelfareConsumptionCoupon(wc);
		wccd.setOperateStatus(OperateStatus.CreditCard);
		welfareConsumptionCouponDetailDao.save(wccd);
		return wc;
	}

	@Override
	public List<WelfareConsumptionCouponDetail> queryAll(WelfareConsumptionCoupon cond, Boolean flag) {
		List<Long> pklist =consumptionCouponDao.queryRule(cond);
		WelfareConsumptionCoupon welfare = new WelfareConsumptionCoupon();
		PropertyUtils.setProperty(welfare, "pkWelfareccIn", pklist);
		List<WelfareConsumptionCoupon> list = consumptionCouponDao.query(welfare);
		List<WelfareConsumptionCoupon> returnlist = new ArrayList<WelfareConsumptionCoupon>();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		for(WelfareConsumptionCoupon we : list){
			String data = sdf.format(we.getEffectiveDate());
			String newDate = sdf.format(new Date());
			//有效日期等于今天
			if(data.equals(newDate)){
				returnlist.add(we);
			}else{
				 long m;
				try {
					m = sdf.parse(newDate).getTime() - sdf.parse(data).getTime();
					//今天减去有效日期大于年
					if((m / (1000 * 60 * 60 * 24)) > 0 || (m / (1000 * 60 * 60 * 24)) == 0){
						returnlist.add(we);
					}
					//今天减去有效日期小于2年
					else{
						continue;
					}
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		}
		List<Long> pk = new ArrayList<Long>();
		for(WelfareConsumptionCoupon coupon : returnlist){
			pk.add(coupon.getPkWelfarecc());
		}
		WelfareConsumptionCouponDetail detail = new WelfareConsumptionCouponDetail();
		PropertyUtils.setProperty(detail, "welfareConsumptionCoupon.pkWelfareccIn", pk);
		if(flag){
			PropertyUtils.setProperty(detail, "operateStatus", OperateStatus.Cash);
		}else{
			PropertyUtils.setProperty(detail, "operateStatus", OperateStatus.CreditCard);
		}
		List<WelfareConsumptionCouponDetail> details = welfareConsumptionCouponDetailDao.query(detail);
		return details;
	}

	@Override
	public Double queryRule(WelfareConsumptionCoupon cond) {
		WelfareConsumptionCoupon we = get(cond.getPkWelfarecc());
		ProductRuleDetail detail = new ProductRuleDetail();
		PropertyUtils.setProperty(detail, "productRule.PkProductRule", we.getTravelContract().getProductRule().getPkProductRule());
		PropertyUtils.setProperty(detail, "seal", false);
		//只有一个在售状态
		ProductRuleDetail details = productDetailManager.query(detail).get(0);
		SettlementsRule rules = new SettlementsRule();
		for(SettlementsRule rule : details.getSettlementsRules()){
			if(rule.getType().name().equals(com.eling.elcms.travel.model.RuleType.Coupon.name())){
				rules = rule;
			}
		}
		String str = rules.getServiceImpl();
		if(str.equals("com.eling.elcms.travel.service.impl.TenCouponServiceImpl")){
			return 0.1d*we.getTravelContract().getPrice();
		}else if(str.equals("com.eling.elcms.travel.service.impl.NineCouponServiceImpl")){
			return 0.09d*we.getTravelContract().getPrice();
		}else if(str.equals("com.eling.elcms.travel.service.impl.EightCouponServiceImpl")){
			return 0.08d*we.getTravelContract().getPrice();
		}else if(str.equals("com.eling.elcms.travel.service.impl.NineBalanceCouponServiceImpl")){
			return 0.09d*we.getTravelContract().getBalance();
		}else if(str.equals("com.eling.elcms.travel.service.impl.EightBalanceCouponServiceImpl")){
			return 0.08d*we.getTravelContract().getBalance();
		}else{
			return 0.0;
		}
	}
	
	
	/**
	 * 有效时间
	 */
	public Date calEffectiveDate(WelfareConsumptionCoupon cond){
		TravelContract travelContract = travelContractManager.get(cond.getTravelContract().getPkTravelContract());
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = sdf.format(travelContract.getSignUpDate());
		String str1 = str.substring(0,4);
		Integer i =Integer.parseInt(str1);
		i=i+2;
		str1 = i.toString();
		str = str.replace((str.substring(0,4)).substring(0,4),str1);
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	@Override
	public List<WelfareccView> queryDetail(List<Long> pklist) {
		return consumptionCouponDao.queryDetail(pklist);
	}
	

	@Override
	public List<WelfareConsumptionCouponStatisticsView> queryStatistics(TravelContract cond) {
		
		Long pkTravelContract = cond.getPkTravelContract();
		
		return consumptionCouponDao.queryStatistics(pkTravelContract);
	}
}
